import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testrole:

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/1_get_role_list.yaml'))
    def test_get_role_list(self, roleinfo, get_env_info):
        # 获取role列表接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default')
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/2_create_role.yaml'))
    def test_create_role(self, roleinfo, get_env_info):
        # 创建role接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default')
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in roleinfo['name']:
            rolename=req.json()['metadata']['name']
            globals()['rolename']=rolename
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/3_get_specific_role.yaml'))
    def test_get_specific_role(self, roleinfo, get_env_info):
        # 获取指定role接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default').replace('{rolename}',globals()['rolename'])
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in roleinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/4_update_role.yaml'))
    def test_update_role(self, roleinfo, get_env_info):
        # 更新指定role接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default').replace('{rolename}',globals()['rolename'])
        method = roleinfo['request']['method']
        roleinfo['request']['data'] = globals()['updatereqbody']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/5_update_section_role.yaml'))
    def test_update_section_role(self, roleinfo, get_env_info):
        # 部分更新指定role的接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default').replace('{rolename}',globals()['rolename'])
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        header = roleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/role/6_delete_specific_role.yaml'))
    def test_delete_specific_role(self, roleinfo, get_env_info):
        # 删除role接口
        baseUrl = get_env_info
        url = baseUrl + roleinfo['request']['url'].replace('{namespace}','default').replace('{rolename}',globals()['rolename'])
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']